package com.kjcxlab.avcviewer;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.kjcxlab.avcviewer.player.VideoPlayer;
import com.kjcxlab.avcviewer.video.VideoStreamInfo;
//NOTE:sps-pps of HTC-M8W
/*byte[] header_sps = { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, (byte) 0x80, 0x1E, (byte) 0xDA, 0x02, (byte) 0x80,
        (byte) 0xF6, (byte) 0x80, 0x6D, 0x0A, 0x13, 0x50 };
		byte[] header_pps = { 0x00, 0x00, 0x00, 0x01, 0x68, (byte) 0xCE, 0x06, (byte) 0xE2 };
*/

public class PlayActivity extends Activity implements SurfaceHolder.Callback {

    private Button sendButton = null;
    private ActionBar mActionBar = null;
    private SurfaceView mSurfaceView1 = null;
    private Surface mSurface = null;
    private VideoPlayer mPlayer = null;
    private VideoStreamInfo mVideoInfo = null;

    //NOTE:sps-pps of MeiZuF
//    byte[] header_sps = {0x00,0x00,0x00,0x01,0x67,0x64,0x00,0x29, (byte) 0xAC,0x1B,0x1A, (byte) 0x80, (byte) 0xA0,0x3D, (byte) 0xA0,0x1E,0x11,0x08, (byte) 0xA7};
//    byte[] header_pps = {0x00,0x00,0x00,0x01,0x68, (byte) 0xEA,0x43, (byte) 0xCB};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set fullscreen
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main);

        mActionBar = getActionBar();
        mActionBar.setDisplayHomeAsUpEnabled(true);

        sendButton = (Button) findViewById(R.id.button1);
        sendButton.setOnClickListener(new sendButtonListener());
        mSurfaceView1 = (SurfaceView) findViewById(R.id.surfaceView1);

        //mSurfaceView1.setZOrderOnTop(true);

        SurfaceViewOnTouchListener mSurfaceListener = new SurfaceViewOnTouchListener();
        mSurfaceView1.setOnTouchListener(mSurfaceListener);
        mSurfaceView1.getHolder().addCallback(this);
        mSurfaceView1.getHolder().setFormat(PixelFormat.TRANSLUCENT);

        mVideoInfo = new VideoStreamInfo();
    }

    class sendButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(getApplicationContext(), "Tv Client starting", Toast.LENGTH_LONG);
            toast.show();
            sendButton.setEnabled(false);
            mPlayer.startPlay();
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_play, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
            case R.id.action_settings:
                return true;
            case R.id.action_about:
                return true;
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    class SurfaceViewOnTouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (mActionBar.isShowing()) {
                mActionBar.hide();
            } else {
                mActionBar.show();
            }
            return false;
        }
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        // this function may be used more than one times after screen rotation
        if (mPlayer == null) {
            mSurface = holder.getSurface();
            mVideoInfo.setSurface(mSurface);
            mVideoInfo.useDefaultConfig(false);
            mPlayer = new VideoPlayer( mVideoInfo);
            Toast toast = Toast.makeText(getApplicationContext(), "Tv Client Created", Toast.LENGTH_LONG);
            toast.show();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }
}
